package cn.sangyy.gift.mqlistener;

import cn.sangyy.gift.constant.MqConstant;
import cn.sangyy.gift.domain.Employee;
import cn.sangyy.gift.domain.Tenant;
import cn.sangyy.gift.mapper.TenantMapper;
import cn.sangyy.gift.utils.BaiduAuditUtils;
import cn.sangyy.gift.utils.SendEmail;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author SangYY
 * @date: 2023/06/24/17:04
 */
@Component
@RocketMQMessageListener(consumerGroup = MqConstant.SERVICE_SYS_CONSUMER_GROUP,topic = MqConstant.TENANT_REGISTER_AUDIT_TOPIC)
public class AuditInfoListener implements RocketMQListener<Tenant> {

    @Autowired
    private TenantMapper tenantMapper;
    @Autowired
    private SendEmail sendEmail;

    @Override
    public void onMessage(Tenant tenant) {
        Employee admin = tenant.getAdmin();
        // 百度智能审核
        Map<String, Object> result = BaiduAuditUtils.censor(tenant.getCompanyName(), tenant.getLogo());
        Boolean success = (Boolean) result.get("success");
        String message = (String) result.get("message");
        if(success){
            tenant.setState(2);
            // 发送邮件激活店铺
            String emailContent = "亲爱的你好，你在蓉礼购平台入驻的"+tenant.getCompanyName()+"店铺审核通过"
                    +"请点击<a href='http://localhost:30010/services/sysmanage/tenant/active/"+tenant.getId()+"'>激活</a>";
            sendEmail.sendMime("蓉礼购店铺入驻激活",emailContent,admin.getEmail(),null);
        }else {
            tenant.setState(1);
            // 发送邮件告诉店铺审核失败
            sendEmail.sendMime("蓉礼购店铺入驻审核不通过",message,admin.getEmail(),tenant.getLogo());
        }
        tenantMapper.updateById(tenant);
    }
}
